<template>
    <el-container class="home-container">
      <!-- 头部区域 -->
      <el-header>
        <div>
          <img src="../assets/heima.png" alt="">
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="toCollapse ? '64px' : '200px'">
          <!-- 折叠展开工具-->
          <div class="toggle-butten" @click="toggleCollapse">|||</div>
          <!-- 导航菜单 -->
          <el-menu background-color="#545c64" text-color="#fff"
          active-text-color="#40A0F5" :unique-opened="true" :collapse="toCollapse"
          :collapse-transition="false" router :default-active="actiovePath">
            <!-- 一级菜单 -->
            <el-submenu :index="item.id+' '" v-for="item in menuList" :key="item.id">
              <!-- 一级菜单模板区域 -->
              <template slot="title" >
                <!-- 字体图标 -->
                <i :class="iconObj[item.id]"></i>
                <!-- 文本 -->
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/'+subitem.path" v-for="subitem in item.children"
              :key="subitem.id" @click="saveNavState('/'+subitem.path)">
                <!-- 二级菜单模板区域 -->
                <template slot="title">
                  <!-- 字体图标 -->
                  <i class="el-icon-menu"></i>
                  <!-- 文本 -->
                  <span>{{subitem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右边主体界面 -->
        <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单数据
      menuList: [],
      // 字体图标
      iconObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 扩展折叠功能
      toCollapse: false,
      // 二级菜单高亮效果
      actiovePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.actiovePath = window.sessionStorage.getItem('actiovePath')
  },
  methods: {
    // 退出登录
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取左侧菜单数据
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      console.log(res)
    },
    // 扩展折叠功能
    toggleCollapse () {
      this.toCollapse = !this.toCollapse
    },
    // 二级菜单高亮效果
    saveNavState (actiovePath) {
      window.sessionStorage.setItem('actiovePath', actiovePath)
      this.actiovePath = actiovePath
    }
  }
}
</script>

<style lang="less" scoped>
  .home-container{
    height: 100%;
  }
  // 头部区域
  .el-header{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 0;
    font-size: 20px;
    color: #E5E7DF;
    >div{
      display: flex;
      align-items: center;
      span{
        margin-left: 10px;
      }
    }
    background-color: #373C40;
  }
  // 侧边栏
  .el-aside{
    background-color: #333644;
    // 折叠展开工具
    .toggle-butten{
      font-size: 12px;
      color: aliceblue;
      line-height: 24px;
      text-align: center;
      // 字体间隔
      letter-spacing: 0.2em;
      font-weight: bold;
      cursor: pointer;
      background-color: #495064;
    }
    // 导航菜单
    .el-menu{
      border-right: none;
    }
  }
  // 右边主体界面
  .el-main{
    background-color: #EAEDF1;
  }
  // 字体图标
  .iconfont{
    margin-right: 5px;
  }
</style>
